//
//  JMAppDelegate.m
//  instructionKits
//
//  Created by Cansing on 02/12/2022.
//  Copyright (c) 2022 Cansing. All rights reserved.
//

#import "JMAppDelegate.h"
#import "JMViewController.h"
#import "JMIOTViewController.h"

#import "AliIOT_loginFlow.h"
#import "JMCommonUtils.h"
#import "AliIOT_RAC.h"
#import "JMIotUtils.h"

//#import "durationSettingViewController.h"
//#import "temperatureSettingViewController.h"
//#import "timingSettingViewController.h"
//#import "editTimingViewController.h"

@interface JMAppDelegate () {
    NSArray *devices;
    NSDictionary *info;
    UIButton *btn;
}

@property(strong, nonatomic) JMViewController *vc;
@property(strong, nonatomic) UINavigationController *nav;

@end

@implementation JMAppDelegate

- (void)dealloc {
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //配置阿里飞燕平台
    [[AliIOT_RAC sharedInstance] iotSetUp:application didFinishLaunchingWithOptions:launchOptions];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor blackColor];

    [self configVC];

    self.nav = [[UINavigationController alloc] initWithRootViewController:self.vc];

    self.window.rootViewController = self.nav;

    [self login];

    [self saveLocation];

    return YES;
}

- (void)configVC {
    self.vc = [JMViewController new];

    self.vc.view.backgroundColor = UIColor.yellowColor;

    btn = [UIButton new];
    btn.enabled = false;
    btn.backgroundColor = UIColor.redColor;
    btn.frame = CGRectMake(100, 100, 50, 50);
    [btn addTarget:self action:@selector(goNext) forControlEvents:UIControlEventTouchUpInside];
    [self.vc.view addSubview:btn];

    UIButton *btn1 = [UIButton new];
    //btn1.backgroundColor = UIColor.systemTealColor;
    [btn1 setImage:[UIImage imageNamed:@"JM_frog_alarm"] forState:0];
    btn1.enabled = false;
    btn1.frame = CGRectMake(100, 200, 50, 50);
    [btn1 addTarget:self action:@selector(goNext1) forControlEvents:UIControlEventTouchUpInside];
    [self.vc.view addSubview:btn1];

    UIButton *btn2 = [UIButton new];
    btn2.enabled = true;
    //btn2.backgroundColor = UIColor.systemTealColor;
    [btn2 setImage:[UIImage imageNamed:@"JM_frog_alarm"] forState:0];
    btn2.frame = CGRectMake(100, 300, 50, 50);
    //[btn2 addTarget:self action:@selector(goNext2) forControlEvents:UIControlEventTouchUpInside];
    [self.vc.view addSubview:btn2];

    /**
     UIButton *btn3 = [UIButton new];
     btn3.backgroundColor = UIColor.redColor;
     btn3.frame = CGRectMake(100, 400, 50, 50);
     [btn3 addTarget:self action:@selector(goNext3) forControlEvents:UIControlEventTouchUpInside];
     [self.vc.view addSubview:btn3];

     UIButton *btn4 = [UIButton new];
     btn4.backgroundColor = UIColor.redColor;
     btn4.frame = CGRectMake(100, 400, 50, 50);
     [btn4 addTarget:self action:@selector(goNext4) forControlEvents:UIControlEventTouchUpInside];
     [self.vc.view addSubview:btn4];
     */
    /*
     UIView *view = btn;

     float centerX = view.bounds.size.width / 2;
     float centerY = view.bounds.size.height / 2;
     float x = view.bounds.size.width / 2;
     float y = view.bounds.size.height;

     CGAffineTransform trans = GetCGAffineTransformRotateAroundPoint(centerX, centerY, x, y, 45.0 / 180.0 * M_PI);
     view.transform = CGAffineTransformIdentity;
     view.transform = trans;
     */
}

- (void)goNext {
    NSLog(@"info = %@", info);
    NSLog(@"devices = %@", devices);
    NSDictionary *device = devices[0];
    JMIOTViewController *vc = (JMIOTViewController *)[NSClassFromString(@"waterPurifierViewController") new];
    vc.iotId = device[@"iotId"];
    vc.navTitle = device[@"productName"];
    vc.token = info[@"token"];
    vc.phoneNum = info[@"mobile"];
    [self.nav pushViewController:vc animated:true];
}

- (void)goNext1 {
    NSLog(@"goNext1");
}

- (void)goNext2 {
    NSLog(@"goNext2");
}

/**
 - (void)goNext3 {
 temperatureSettingViewController *vc = [temperatureSettingViewController new];
 [self.nav pushViewController:vc animated:true];
 }

 - (void)goNext4 {
 editTimingViewController *vc = [editTimingViewController new];
 [self.nav pushViewController:vc animated:true];
 }
 */

/*
 CGAffineTransform GetCGAffineTransformRotateAroundPoint(float centerX, float centerY, float x, float y, float angle) {
 x = x - centerX;  //计算(x,y)从(0,0)为原点的坐标系变换到(CenterX ，CenterY)为原点的坐标系下的坐标
 y = y - centerY;  //(0，0)坐标系的右横轴、下竖轴是正轴,(CenterX,CenterY)坐标系的正轴也一样

 CGAffineTransform trans = CGAffineTransformMakeTranslation(x, y);
 trans = CGAffineTransformRotate(trans, angle);
 trans = CGAffineTransformTranslate(trans, -x, -y);
 return trans;
 }
 */

#pragma mark - IOT Login

- (void)login {
    __weak __typeof(self) weakSelf = self;
    [[AliIOT_loginFlow sharedInstance]
        loginWithName:@"13430820264"  //@"15659293825"
             withPass:@"zj123456"     //@"yy123456"
           completion:^(id responseObject, _Bool isSuccess, NSError *error) {
             __strong __typeof(weakSelf) strongSelf = weakSelf;
             if (isSuccess && !error) {
                 strongSelf->info = [[NSDictionary alloc] initWithDictionary:responseObject];
#define KEY_ACCOUNT_TOKEN @"accountToken"
                 [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"token"]
                                                           forKey:KEY_ACCOUNT_TOKEN];
                 [[NSUserDefaults standardUserDefaults] synchronize];
                 [self loadDeviceListWithCompletionHandler];
             }
           }];
}

- (void)loadDeviceListWithCompletionHandler {
    [JMIotUtils loadDeviceListWithCompletionHandler:^(NSError *error, NSArray *list) {
      //NSLog(@"list = %@", list);
      self->devices = [[NSArray alloc] initWithArray:list];
      self->btn.enabled = true;
      self->btn.backgroundColor = UIColor.blueColor;

      for (NSDictionary *dic in list) {
          NSString *iotId = dic[@"iotId"];
          [self listBindingByDev:iotId];
      }
    }];

    [JMIotUtils generateShareQrCode:@[ @"BUdXbCUEplOjNtI7Nbqp000000" ] completionHandler:^(NSError *error, id data) {
      NSLog(@"%@", data);
    }];
}

- (void)listBindingByDev:(NSString *)iotId {
    [JMIotUtils listBindingByDev:iotId completionHandler:^(NSError *error, id data) {
      NSLog(@"%@", data);
      NSLog(@"cansing");
    }];
}

- (void)saveLocation {
    //保存 location
    NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
    mdic[@"provinceId"] = @"440000";
    mdic[@"province"] = @"广东省";
    mdic[@"provinceName"] = @"广东省";
    mdic[@"cityId"] = @"440300";
    mdic[@"city"] = @"深圳市";
    mdic[@"cityName"] = @"深圳市";
    mdic[@"districtId"] = @"440305";
    mdic[@"district"] = @"南山区";
    mdic[@"areaId"] = @"440305";
    mdic[@"areaName"] = @"南山区";
    [[NSUserDefaults standardUserDefaults] setObject:mdic forKey:@"JMGLOBALLOCATION"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
